import cv2
import numpy
from typing import List, Set
from .models import FrameData
import numpy as np

# 视频处理器
class VideoProcessor:
    # 初始化视频处理器，接收视频路径、开始时间和结束时间
    def __init__(self, video_path: str, start_time: float, end_time: float):
        self.cap = cv2.VideoCapture(video_path)
        self.fps = self.cap.get(cv2.CAP_PROP_FPS)
        self.total_frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
        video_duration = self.total_frames / self.fps
        self.start_frame = int(start_time * self.fps)
        self.end_frame = int((video_duration - end_time) * self.fps)
        
    # 实现上下文管理器的进入方法
    def __enter__(self):
        return self

    # 实现上下文管理器的退出方法，释放视频捕获资源
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.cap.release()

    # 获取指定时间段内的帧
    def get_frames(self) -> List[np.ndarray]:
        """返回原始BGR帧，而非处理后的LUV帧"""
        frames = []
        self.cap.set(cv2.CAP_PROP_POS_FRAMES, self.start_frame)
        for _ in range(self.start_frame, self.end_frame):
            success, frame = self.cap.read()
            if not success:
                break
            frames.append(frame)  # 保持原始BGR格式
        return frames




